#include "gamescene.h"
#include "ui_gamescene.h"
#include "QDebug"
#include "QPainter"
#include "mypushbutton.h"
#include "QTimer"
#include "QLabel"
#include "dataconfig.h"
#include "QPropertyAnimation"
#include "QSound"
//gamescene::gamescene(QWidget *parent) :
//    QMainWindow(parent),
//    ui(new Ui::gamescene)
//{
//    ui->setupUi(this);
//}
gamescene::gamescene(int levelNum):ui(new Ui::gamescene){
     ui->setupUi(this);

     QSound *winSound=new QSound(":/res/LevelWinSound.wav",this);
      QSound *iconSound=new QSound(":/res/ConFlipSound.wav",this);

     this->levelIndex=levelNum;
    this->isWin=false;
     this->setFixedSize(320,588);
    this->setWindowTitle("翻金币");
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
     connect(ui->actionquit,&QAction::triggered,[=](){
         this->close();
     });
     myPushButton *BackBtn=new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    BackBtn->setParent(this);
    BackBtn->move(this->width()-BackBtn->width(),this->height()-BackBtn->height());
    connect(BackBtn,&QPushButton::clicked,[=](){
        QTimer::singleShot(100,this,[=](){
            emit this->chooseScenceBack();
        });
    });
    //显示关卡
    QLabel *label=new QLabel(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString str=QString("Level:%1").arg(levelIndex);
    label->setFont(font);
    label->setGeometry(25,this->height()-50,120,50);
    label->setText(str);
    dataConfig dataconfig(this);
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=dataconfig.mData[levelIndex][i][j];
        }
    }

    //设置胜利图片
    QLabel *winLabel=new QLabel(this);
    QPixmap winpix;
    winpix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,winpix.width(),winpix.height());
    winLabel->setPixmap(winpix);
    //先把它藏在上面
    winLabel->move((this->width()-winpix.width())*0.5,-winpix.height());

    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QPixmap pix=QPixmap(":/res/BoardNode(1).png");
             QLabel *label=new QLabel(this);
             label->setGeometry(0,0,pix.width(),pix.height());
             label->setPixmap(pix);
             label->move(57+i*50,200+j*50);
             QString str;
             if(gameArray[i][j]==1){
                 str=":/res/Coin0001.png";
             }
             else{
                  str=":/res/Coin0008.png";
             }
             mycoin *coin=new mycoin(str);
             coin->setParent(this);
             coin->move(59+i*50,200+j*52);
             coin->posx=i;
             coin->posy=j;
             coin->flag=gameArray[i][j];
             //可以创建按钮数组来放对应的对象，后期也可以使用
             coinBtn[i][j]=coin;
             connect(coin,&mycoin::clicked,coin,[=](){
                 iconSound->play();
                 coin->changFlag();
                 this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                 if(coin->posx+1<=3){
                     coinBtn[coin->posx+1][coin->posy]->changFlag();
                     this->gameArray[coin->posx+1][coin->posy]=this->gameArray[coin->posx+1][coin->posy]==0?1:0;
                 }
                 if(coin->posx-1>=0){
                     coinBtn[coin->posx-1][coin->posy]->changFlag();
                     this->gameArray[coin->posx-1][coin->posy]=this->gameArray[coin->posx-1][coin->posy]==0?1:0;
                 }
                 if(coin->posy+1<=3){
                     coinBtn[coin->posx][coin->posy+1]->changFlag();
                     this->gameArray[coin->posx][coin->posy+1]=this->gameArray[coin->posx][coin->posy+1]==0?1:0;
                 }
                 if(coin->posy-1>=0){
                     coinBtn[coin->posx][coin->posy-1]->changFlag();
                     this->gameArray[coin->posx][coin->posy-1]=this->gameArray[coin->posx][coin->posy-1]==0?1:0;
                 }
                 this->isWin=true;
                 for(int i=0;i<4;i++){
                     for(int j=0;j<4;j++){
                         if(coinBtn[i][j]->flag==false){
                             this->isWin=false;
                             break;
                         }
                     }
                 }
                 if(this->isWin==true){
                     QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                     animation->setDuration(1000);
                     //设置开始位置
                     animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                     //设置结束位置
                     animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                     //设置缓和曲线
                     animation->setEasingCurve(QEasingCurve::OutBounce);
                     animation->start();
                     winSound->play();
                     for(int i=0;i<4;i++){
                         for(int j=0;j<4;j++){
                                 coinBtn[i][j]->isWin=true;
                             }
                     }
                }
             });
        }
    }


}


void gamescene::paintEvent(QPaintEvent*){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/res/Title.png");
    //可以通过绘图设备来设置图片的大小
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}


gamescene::~gamescene()
{
    delete ui;
}
